<?
include("common/header.html");
include("common/page.php");
include("common/left.html");
if(!$_SESSION['memberID']){exit("<script>window.location='dlogin.html'</script>");}
$ac=$GLOBALS['_ac'];
$pw=$GLOBALS['_pw'];
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));
$currentPage=$_REQUEST["currentPage"]?$_REQUEST["currentPage"]:1;
$pageSize=$_REQUEST['pageSize']?$_REQUEST["pageSize"]:7;
$title=$_REQUEST['title'];
$order=isset($_REQUEST['order'])?$_REQUEST["order"]:1;
$by=isset($_REQUEST['by'])?$_REQUEST["by"]:1;
$pid=$_REQUEST['pid']?$_REQUEST["pid"]:"";
$time=$_REQUEST['time'];
$status=$_REQUEST['status']?$_REQUEST["status"]:array();
$zf=$_REQUEST['zf']?$_REQUEST["zf"]:array();
$op=$_REQUEST['op']?$_REQUEST["op"]:date("Y-m-d",strtotime("-1 week"));
$ed=$_REQUEST['ed']?$_REQUEST["ed"]:date("Y-m-d");
$tj=$_REQUEST['tj'];
$txt=$_REQUEST['txt']?$_REQUEST["txt"]:"";
$firm=$_REQUEST['firm']?$_REQUEST["firm"]:array();
//要提交的数据
$salerid=$title=$sub_op=$sub_ed=$play_op=$play_ed=$ok_op=$ok_ed=$ordid=$name=$tel=$pay=$from=$distal=$ltype=$city=$day=$hotel=$traffic=$canc=$payinfo=$tid=$UUttitle=$remotenum=$origin=$p_type=$confirm=$aid="";
$concat=1;
$from=$m_info['dcode'];
$search=$from?3:2;
$ordtime=array();
$ol=array();
$add=array();
//查询时间
if($op){
	$_op=explode("-",$op);
	if(!checkdate($_op[1],$_op[2],$_op[0])){	
		$op="";
	}
}
if($ed){
	$_ed=explode("-",$ed);
	if(!checkdate($_ed[1],$_ed[2],$_ed[0])){	
		$ed="";
	}
}
if($op==date("Y-m-d") && $ed==date("Y-m-d")){
	$tp=0;
}
elseif($op==date("Y-m-d",strtotime("-1 days")) && $ed==date("Y-m-d",strtotime("-1 days"))){
	$tp=1;
}
elseif($op==date("Y-m-d",strtotime("+1 days")) && $ed==date("Y-m-d",strtotime("+1 days"))){
	$tp=2;
}
elseif($op==date("Y-m-d",strtotime("-".(date("N")-1)." days")) && $ed==date("Y-m-d",strtotime("+".(7-date("N"))." days"))){
	$tp=3;
}
elseif($op==date("Y-m-d",strtotime("-".(date("N")-1+7)." days")) && $ed==date("Y-m-d",strtotime("-".date("N")." days"))){
	$tp=4;
}
elseif($op==date("Y-m-01") && $ed==date("Y-m-d",strtotime(date("Y-m-01")."+1 month -1 days"))){
	$tp=5;
}
elseif($op==date("Y-m-01",strtotime("-1 month")) && $ed==date("Y-m-d",strtotime(date("Y-m-01",strtotime("-1 month"))."+1 month -1 days"))){
	$tp=6;
}
switch($time){
	case 0:	$sub_op=$op;
			$sub_ed=$ed." 23:59:59";
			break;
	case 1:	$play_op=$op;
			$play_ed=$ed." 23:59:59";
			break;
	case 2:	$ok_op=$op;
			$ok_ed=$ed." 23:59:59";
			break;
}
//查询条件
/*if($tj!=null && filter_var($tj, FILTER_VALIDATE_INT)===false){
	echo "<script>alert('所选条件不可用!');</script>";
	exit;
}
$selcss="display:none;";
if($txt && p_match($txt)){
	$txt="";
}
switch($tj){
	case 0:	$ordid=$txt;
			break;
	case 1:	$title=$txt;
			break;
	case 2:	$name=$txt;
			break;
	case 3:	$tel=$txt;
			break;
	case 4:	$queryIds=array($txt);
			$txtcss="style=\"display:none\"";
			$selcss="";
			break;
}*/
//状态
$_status=array(0,1,2,3,4,5,6,7);
if(array_diff($status,$_status))$status=0;
//余额支付
$_balance=array("",1,2,3);
if(in_array(2,$zf)){
	$money="checked";
	$balance=2;
}
if(in_array(3,$zf)){
	$tmoney="checked";
	$balance=3;
}
if(array_diff($zf,$_balance))$balance="";
elseif($money && $tmoney){
	$balance="";
}
//排序
$_order=array(1,2,3,4,5,6);
if(!in_array($order,$_order))$order=1;
if($by!=1 && $by!=0)$by=1;
//确认状态
$_firm=array(0,1,2,3,4);
if(array_diff($firm,$_firm))$firm=array(1);
//下级信息
$dnames=$sids=array();
$low[]=array("id"=>$admin_info['id'],"name"=>$admin_info['dname'],"sel"=>($tj==4 && $txt==$admin_info['id']?"selected":""));
$str="select a.id as id,a.dname as dname,a.sid as sid,a.lvl as lvl from uu_distributor a  left join uu_distributor b on a.sid=b.id where a.oid=".$_SESSION["uu_distoid"];
$GLOBALS['le']->query($str);
while($row=$GLOBALS['le']->fetch_assoc()){
	$sids[$row['id']]=$row['sid'];
	if($row['sid']==$m_info['adminid'] && $row['lvl']!=2){
		$_sel=$tj==4 && $txt==$row['id']?"selected":"";
		$low[]=array("id"=>$row['id'],"name"=>$row['dname'],"sel"=>$_sel);}
	$dnames[$row['id']]=$row['dname'];
}
if(!$queryIds){
	$queryIds=array();
	$topId=$m_info['adminid'];
	foreach($dnames as $id=>$dname){
		$sid=$id;
		do{
			if($sid==$topId) break;
		}while($sid=$sids[$sid]);
		if($sid!=$topId) continue;
		$queryIds[]=$id;
	}
}
$lqueryIds=array_diff($queryIds,array($m_info['adminid']));
$queryIds=implode(",",$queryIds);
$lqueryIds=implode(",",$lqueryIds);
//echo "''".",'".$queryIds."',"."''".",'".$tid."','".$title."','".$UUttitle."','".$sub_op."','".$sub_ed."','".$play_op."','".$play_ed."','".$ok_op."','".$ok_ed."','".$ordid."','".$name."','".$tel."','".implode("|",$status)."','".$pay."','".$from."','".$order."','".$by."','".($currentPage-1)*$pageSize."','".$totalOrder."','".(int)$return."','".$search."','".$payinfo."','".(int)$pmode."','".$remotenum."','".$origin."','".$p_type."','".$confirm."','".$aid."',".(int)$concat.",".(int)$ifpack;
//门票订单总数
$count=$s2->Order_Globle_Search("",(string)$queryIds,"",$tid,$title,$UUttitle,$sub_op,$sub_ed,$play_op,$play_ed,$ok_op,$ok_ed,$ordid,$name,$tel,implode("|",$status),$pay,$from,$order,$by,($currentPage-1)*$pageSize,$pageSize,2,(int)$search,$payinfo,(int)$pmode,$remotenum,$origin,$p_type,$confirm,$aid,(int)$concat,(int)$ifpack);
$count=explode(",",$count);
$totalOrder=$count[0];
$totalTnum=$count[1];
$totalMoney=$count[2];
//门票订单查询
$xml=$s2->Order_Globle_Search("",$queryIds,"",$tid,$title,$UUttitle,$sub_op,$sub_ed,$play_op,$play_ed,$ok_op,$ok_ed,$ordid,$name,$tel,implode("|",$status),$pay,$from,$order,$by,($currentPage-1)*$pageSize,$pageSize,(int)$return,$search,$payinfo,(int)$pmode,$remotenum,(int)$origin,$p_type,$confirm,$aid,(int)$concat,(int)$ifpack);
if($xml==105){
}elseif(judge($xml)){
	exit(judge($xml));
}else{
	$result=simplexml_load_string($xml);
}
//var_dump($result);
if($ordid){
	if(!in_array((string)$result->Rec[0]->UUmid,explode(",",$queryIds))){
		$result=array();    //查询权限
		$totalOrder=0;
		$totalTnum=0;
		$totalMoney=0;
	}
}
//门票
$r=array();
$orders=array();
foreach($result as $row){
	$orders[]=$row->UUordernum;
}
if(count($orders)!=0){
	//利润成本
	$orders=implode(",",$orders);
	
	$str="select orderid,sum(dlirun) as dlirun from uu_fx_lirun where gmid in ($queryIds) and orderid in ($orders)
	group by orderid";
	$GLOBALS['le']->query($str);
	$orderliruns=array();
	while($row=$GLOBALS['le']->fetch_assoc()){
		$orderliruns[$row['orderid']]=$row['dlirun'];
	}
	if($lqueryIds){
		$str="select orderid,sum(dlirun) as dlirun from uu_fx_lirun where gmid in ($lqueryIds) and orderid in ($orders)
		group by orderid";
		$GLOBALS['le']->query($str);
		$lorderliruns=array();
		while($row=$GLOBALS['le']->fetch_assoc()){
			$lorderliruns[$row['orderid']]=$row['dlirun'];
		}
	}
	
	$employers=array();
	$str="select ordernum,employer from uu_fx_employer_order where ordernum in ($orders)";
	$GLOBALS['le']->query($str);
	while($row=$GLOBALS['le']->fetch_assoc()){
		$employers[$row['ordernum']]=$dnames[$row['employer']];
	}
	
	//客源地
	$origin=array();
	if($result->Rec[0]->UUlid==406){
		$sel="SELECT `orderid`,`origin` FROM `uu_order_fx_details` WHERE `orderid` IN(".$orders.")";
		$GLOBALS['le']->query($str);
		while($row=$GLOBALS['le']->fetch_assoc()){
			$origin[$row['orderid']]=$row['origin'];
		}
	}
}
function S2A($ord){
	global $admin_info,$uu,$uc,$employers,$dnames;
	$t=array();
	$t['title']=$t['title2']=$ord->UUltitle;
	$t['ordid']=$t['ordid2']=$t['ordid3']=$t['ordid4']=$t['ordid5']=$t['ordid6']=$t['ordid7']=$t['ordid8']=$t['ordid9']=$t['ordid10']=$t['ordid11']=$t['ordid12']=$ord->UUordernum;
	$t['num']=$t['num2']=$ord->UUtnum;
	$t['fpri']=(int)$ord->UUtnum?(float)number_format($ord->UUtprice-$orderliruns[(string)$ord->UUordernum]/100/$ord->UUtnum,2,".",""):0;
	$jsprice=(int)$ord->UUtnum?(float)number_format($ord->UUtprice-$lorderliruns[(string)$ord->UUordernum]/100/$ord->UUtnum,2,".",""):0;
	$t['pri']=$jsprice;
	$t['total']=$t['total2']=(int)$ord->UUtnum*(float)$jsprice;
	$t['name']=$ord->UUordername;
	$t['tel']=$ord->UUmid==$admin_info['id']?$ord->UUordertel:substr($ord->UUordertel,0,3)."****".substr($ord->UUordertel,7);
	$t['opname']=$employers[(string)$ord->UUordernum]?$employers[(string)$ord->UUordernum]:mbsubstr($dnames[(string)$ord->UUmid],15,"...");
	$t['sub']=substr($ord->UUordertime,0,10);
	$t['subtime']=substr($ord->UUordertime,0,10)."<br />".substr($ord->UUordertime,11);
	$t['play']=$ord->UUbegintime;
	$t['finish']=$ord->UUdtime=="0000-00-00 00:00:00"?"":$ord->UUdtime;
	$t['ok']=substr($ord->UUdtime,0,10)!="0000-00-00"?substr($ord->UUdtime,0,10):"未验证";
	$t['remsg']=$t['remsg2']=$ord->UUremsg;
	if($ord->UUpmode==0){$t['i_txt']="单位结算";$t['i_css']="green";}
	elseif($ord->UUpmode==1){$t['i_txt']=$ord->UUpaystatus==0?"现场支付":"账户余额";$t['i_css']="orange";}
	elseif($ord->UUpmode==2){$t['i_txt']="信用额度";$t['i_css']="fuchsia";}
	elseif($ord->UUpmode==4){$t['i_txt']="手机下单";$t['i_css']="";}
	$t['pstatus']=$ord->UUpmode!=0 && $ord->UUpaystatus==2?"<br /><font color='red'>(未支付)</font>":"";
	$t['order_num1']=$t['order_num2']=$t['order_num3']=$t['order_num4']=$t['order_num5']=$t['order_num6']=$t['order_num7']=1;
	$t['canc']="";
	$t['send']="";
	$t['upd']="";
	$t['print']="";
	$t['regain']="";
	$t['updfun']="alterOrder";
	$t['cancfun']="cancelOrder";
	$t['updtxt']="修改";
	$t['canctxt']="取消";
	$t['msg']="";
	$t['see']="orderCheck.html?ordernum";
	switch($ord->UUstatus){
		case 0:	$t['s_txt']="未使用";
				$t['s_css']="unused";
				$t['print']="show";
			//	$t['msg']="show";
				if(($ord->UUmid==$admin_info['id'] || $admin_info['lvl']==4) && $ord->UUpmode!=4 && ($ord->UUpaystatus==1 || $ord->UUpaystatus==0) && $ord->UUdname!="游客" && strlen((string)$ord->UUordernum)<=6){
					if($ord->UUorder_confirm==0 || $ord->UUorder_confirm==1){
						$t['canc']="show";
					}
					elseif($ord->UUorder_confirm==2 && !in_array((string)$ord->UUordernum,$uu)){
						$t['canctxt']="申请取消";
						$t['cancfun']="app_cancel";
						$t['canc']="show";
					}
				}
				if((str_replace("ss|","",$ord->UUmid)==$admin_info['id'] || $admin_info['lvl']==4) && (int)$ord->UUremsg<3 && $ord->UUpaystatus==1 && $ord->UUdname!="游客")$t['send']="show";
				if($ord->UUmid==$admin_info['id'] && (int)$ord->UUremsg<3 && (int)$ord->UUaid!=1600 && $ord->UUpaystatus==1 && strlen((string)$ord->UUordernum)<=6){
					if($ord->UUorder_confirm==0 || $ord->UUorder_confirm==1){
						$t['updfun']="alterOrder";
						$t['upd']="show";
					}
					elseif($ord->UUorder_confirm==2 && !in_array((string)$ord->UUordernum,$uu)){
						$t['updtxt']="申请修改";
						$t['updfun']="app_alter";
						$t['upd']="show";
					}
				}
				break;
		case 1:	$t['s_txt']="已使用";
				$t['s_css']="used";
				break;
		case 2:	$t['s_txt']="已过期";
				$t['s_css']="outdate";
				if(($ord->UUmid==$admin_info['id'] || $admin_info['lvl']==4) && $ord->UUpmode!=4 && ($ord->UUpaystatus==1 || $ord->UUpaystatus==0) && ($ord->UUorder_confirm==0 || $ord->UUorder_confirm==1) && $ord->UUdname!="游客")$t['canc']="show";
				break;
		case 3:	$t['s_txt']="已取消";
				$t['s_css']="cancel";
			//	if($ord->UUmid==$admin_info['id']&&$m_info['lvl']!=4)$t['regain']="show";
				break;
		case 4:	$t['s_txt']="被替代";
				$t['s_css']="used";
				break;
		case 5:	$t['s_txt']="被修改";
				$t['s_css']="";
				break;
		case 6:	$t['s_txt']="已撤销";
				$t['s_css']="cancel";
				break;
		case 7:	$t['s_txt']="部分使用";
				$t['s_css']="used";
				break;
	}
	$t['pay']="";
	if($ord->UUpaystatus==2 && (str_replace("ss|","",$ord->UUmid)==$admin_info['id'] || $admin_info['lvl']==4) && $ord->UUorder_confirm==2){
		$t['pay']="show";
	}
	$t['firm']="";
//	echo $ord->UUorder_confirm."<br />";
	if((int)$ord->UUorder_confirm==1){
		$t['firm']="<br />(未确认)";
	}
	elseif((int)$ord->UUorder_confirm==4){
		$t['firm']="<br /><font color='red'>(已拒绝)</font>";
	}
	elseif((int)$ord->UUorder_confirm!=0){
		$t['firm']="<br /><font color='green'>(已确认)</font>";
	}
	$t['pmode']=$ord->UUpmode;
	$t['ttitle']=$ord->UUttitle;
	$t['ttitle2']=$ord->UUttitle;
	return $t;
}
$ac=$rc=$au=$ru=$tx=0;
$uu=array();
$sel="select orderid,status from uu_order_change_apply where did=".$admin_info['id'];//reply_time between '".$op."' and '".$ed."' and 
$GLOBALS['le']->query($sel);
while($row=$GLOBALS['le']->fetch_assoc()){
	switch($row['status']){
		case 0:	$uu[]=$row['orderid'];break;
		case 1:	$uu[]=$row['orderid'];break;
		case 2:	$ac++;$tx=1;break;
		case 3:	$rc++;$tx=1;break;
		case 4:	$uu[]=$row['orderid'];break;
		case 5:	$au++;$tx=1;break;
		case 6:	$ru++;$tx=1;break;
	}
}
$i=0;
foreach($result as $ord){
	$t=S2A($ord);
	$ol[$i]=$t;
	$i++;
}
$a3=array();
$i=0;
foreach($result as $ord){
	if($ord->UUconcat_id=="main"){
		$concat=true;
		$main=$s2->Order_Globle_Search("","","","","","","","","","","","",(string)$ord->UUordernum);
		$temp=array();
		foreach(simplexml_load_string($main) as $v){
			if((string)$v->UUordernum!=(string)$ord->UUordernum){
				$t=S2A($v);
				$temp[]=$t;
				$ol[$i]['total']=$ol[$i]['total2']=$ol[$i]['total']+$t['total'];
				$ol[$i]['order_num1']=$ol[$i]['order_num2']=$ol[$i]['order_num3']=$ol[$i]['order_num4']=$ol[$i]['order_num5']=$ol[$i]['order_num6']=$ol[$i]['order_num7']=$ol[$i]['order_num1']+$t['order_num1'];
			}
		}
		$a3[]=$temp;
	}
	else{$a3[]=array();}
	$i++;
}
include("tpl/bought.html");
include("common/footer.html");?>